#include <REGX52.H>

sbit LCD_RW = P2^5;
sbit LCD_RS = P2^6;
sbit LCD_E  = P2^7;
#define LCD_DataPort P0

/*

	VSS	  地
	VDD   电源正极
	VO    对比度调节电压
	RS    数据/指令选择，1为数据，0为指令
	RW    读/写选择，1为读，0为写
	E     使能，1为数据有效，下降沿执行命令
	D0~D7 数据输入/输出
	A     背光灯电源正极
	K     背光灯电源负极
	
*/


void LCD_Delay()		//@11.0592MHz  1ms
{
	unsigned char i, j;	//一个私有的延时函数，代码好移植，直接改参数

//	_nop_();
	i = 2;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
}

void LCD_WriteCommand(unsigned char Command)
{
	LCD_RS = 0;	//指令
	LCD_RW = 0;	//写
	LCD_DataPort = Command;		//要写的指令
	LCD_E = 1;	//要延时一段时间
	LCD_Delay();
	LCD_E = 0;
	LCD_Delay();
}

void LCD_WriteData(unsigned char Data)
{
	LCD_RS = 1;	//数据
	LCD_RW = 0;	//写
	LCD_DataPort = Data;	//要写的数据
	LCD_E = 1;	//要延时一段时间
	LCD_Delay();
	LCD_E = 0;
	LCD_Delay();
}

void LCD_Init(void)				//初始化
{
	LCD_WriteCommand(0x38);		//八位数据接口，两行显示，5*7点阵
	LCD_WriteCommand(0x0C);		//显示开，光标关，闪烁关
	LCD_WriteCommand(0x06);		//数据读写操作后，光标自动加一，画面不动
	LCD_WriteCommand(0x01);		//清屏
}


void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
	if(Line == 1)
	{
		LCD_WriteCommand( 0x80 | (Column-1) );		//第一行//设置光标位置
	}
	else
	{
		LCD_WriteCommand( 0x80 | (Column-1) + 0x40 );//第2行
	}
}

void LCD_ShowChar(unsigned char Line,unsigned char Column,unsigned char Char)
{	
	/* 显示字符
	发送指令 0x80|AC	//设置光标位置
	发送数据			//发送要显示的字符数据
	发送数据			//发送要显示的字符数据
	……
	
*/
	LCD_SetCursor(Line,Column);
	LCD_WriteData(Char);
}


void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned char *String)
{
	unsigned char i;
	
	LCD_SetCursor(Line,Column);
	for(i=0;String[i] != '\0';i++)//遍历数组，写每一位
	{
		LCD_WriteData(String[i]);
	}	
}

int LCD_Pow(int X,int Y)	//X的Y次幂 函数
{
	unsigned char i;
	int Result = 1;
	for(i=0;i<Y;i++)
	{
		Result*=X;
	}
	return Result;
}



void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{	//无符号的数字
	unsigned char i;
	LCD_SetCursor(Line,Column);
	for(i=Length;i>0;i--)//	反着遍历
	{
		LCD_WriteData('0'+Number/LCD_Pow(10,i-1)%10);	//'0'+这个数，转化成字符的数字
	}
}

void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{	//有符号的数字
	unsigned char i;
	unsigned int Number1;
	LCD_SetCursor(Line,Column);
	if(Number>=0)
	{
		LCD_WriteData('+');
		Number1 = Number;
	}
	else
	{
		LCD_WriteData('-');
		Number1 = -Number;
	}
	for(i=Length;i>0;i--)//	反着遍历
	{
		LCD_WriteData('0'+Number1/LCD_Pow(10,i-1)%10);	//'0'+这个数，转化成字符的数字
	}
}

void LCD_ShowHexNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{	//显示十六进制数
	unsigned char i;
	unsigned char SingleNumber;
	LCD_SetCursor(Line,Column);
	for(i=Length;i>0;i--)//	反着遍历
	{
		SingleNumber = Number/LCD_Pow(16,i-1)%16;	//先取出这个十六进制数
		if(SingleNumber < 10)						//判断是否小于10
			LCD_WriteData('0'+SingleNumber);		//显示出来
		else
			LCD_WriteData('A'+SingleNumber-10);		//偏移(要减10)，才能显示十六进制ABCDEF
	}
}


void LCD_ShowBinNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{	//显示二进制数字
	unsigned char i;
	LCD_SetCursor(Line,Column);
	for(i=Length;i>0;i--)//	反着遍历
	{
		LCD_WriteData('0'+Number/LCD_Pow(2,i-1)%2);	//'0'+这个数，转化成字符的数字
	}	
	
}